#coding:utf-8
import pickle,sys,mysql.connector

def read_password():
    with open('pass.txt','rb') as f:
        return pickle.load(f)

def dump_password(list):
    with open('pass.txt','wb+') as f:
        pickle.dump(list,f)
        return True

def read_tags():
    with open('tags.txt','rb') as f:
        return pickle.load(f)

def dump_tags(list):
    with open('tags.txt','wb+') as f:
        pickle.dump(list,f)
        return True


def main():
    count = 3
    while count:
        _root_password = raw_input('请输入主密码(您还有% s次机会):' % count)
        #with open('e:/pass.txt','r') as f:
        _password_list = read_password()
        for i in _password_list:
            if i['pas_obj'] == 'root' and i['_password'] == _root_password:
                return('登录成功')
        count -= 1
    sys.exit('锁定')


def _choice():
    while True:
        choice = raw_input('''
            1、显示所有密码
            2、修改密码
            3、添加鑫的密码
            4、添加新的分类
            5、删除密码
            6、修改分类
            7、删除分类
            8、生成随机密码
            9、导出密码文件
            10、显示所有分类
            有以上几个选择：''')
        if choice == '1':
            #sql = 'select * from md_code'
            #sql_help = sql_action()
            #aa = sql_help._sql_selectall(sql)
            #print aa
            print read_password()

        elif choice == '2':
            #aa = Pwd()
            updata_Pwd()
        elif choice == '3':
            #aa = Pwd()
            add_Pwd()
            #return aa
        elif choice == '4':
            #aa = tags()
            add_tags()
        elif choice == '5':
            #aa = Pwd()
            delete_Pwd()
        elif choice == '6':
            #aa = tags()
            update_tags()
        elif choice == '7':
            #aa = tags()
            delete_tags()
        elif choice == '10':
            # aa = tags()
            print read_tags()
        else:
            sys.exit('选择错误，退出')


def add_Pwd():
    Flag = 1
    while Flag:
        tag = raw_input('请输入你需要添加密码的类型：')
        tags_type = read_tags()
        for i in tags_type:
            if i.get('tag') == tag:
                print '请输入以下密码项目,',i['items'],':',
                item = raw_input()
                dict={'tag':tag}
                item_each = item.split(',')
                for j in range(len(i['items'])):
                    dict[i['items'][j]]=item_each[j]
                _password_list=read_password()
                _password_list.append(dict)
                dump_password(_password_list)
                print _password_list
                Flag = 0
                break
        else:
            continue

def updata_Pwd():
    Flag = 1
    while Flag:
        title = raw_input('请输入你需要修改密码的title：')
        accounts = raw_input('请输入你需要修改密码的accounts：')
        _password_list= read_password()
        for i in _password_list:
            if i.get('accounts') == accounts:
                password = raw_input('请输入你要修改的密码')
                i['password'] = password
                dump_password(_password_list)
                Flag = 0
                break
        else:
            print 'title或accounts 不存在，请重新输入'
            continue


def delete_Pwd():
    Flag = 1
    while Flag:
        tag = raw_input('请输入你需要删除的密码的tag：')
        title = raw_input('请输入你需要删除的密码的title：')
        accounts = raw_input('请输入你需要删除的密码的accounts：')
        _password_list = read_password()
        for i in _password_list:
            if i.get('tag') == tag and i.get('title') == title and i.get('accounts') == accounts:
                _password_list.remove(i)
                dump_password(_password_list)
                Flag = 0
                break
        else:
            continue


def add_tags():
    Flag = 1
    while Flag:
        tag_name = raw_input('请输入密码类型名称：')
        tag_item = raw_input('请输入新增密码类型的项目(以逗号间隔)：')
        tags_type = read_tags()
        for i in tags_type:
            if i.get('tag') == tag_name:
                print '存在'
                break
        else:
            tags_type.append({'tag':tag_name,'items':tag_item.split(',')})
            dump_tags(tags_type)
            break
    else:
        pass

def update_tags():
    Flag = 1
    while Flag:
        tag_name = raw_input('请输入密码类型名称：')
        for i in tags_type:
            if i.get('tag') == tag_name:

                Flag = 0
                break
        else:
            print '不存在'
            continue
#

def delete_tags():
    Flag = 1
    while Flag:
        tag_name = raw_input('请输入密码类型名称：')
        tags_type = read_tags()
        for i in tags_type:
            if i.get('tag') == tag_name:
                tags_type.remove(i)
                dump_tags(tags_type)
                Flag = 0
                break
        else:
            print '输入的分类不存在，请重新输入！'
            continue

if __name__ == '__main__':
    print main()
    _choice()

